package csc.fresher.view;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.service.InternetLoginService;

@ManagedBean
@RequestScoped
public class InternetProfileManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value="#{internetLoginServiceImpl}")
	private InternetLoginService loginService;
	
	@ManagedProperty(value="#{internetLoginManageBean}")
	private InternetLoginManageBean login;
	@ManagedProperty(value="#{customerManageBean}")
	private CustomerManageBean customerManageBean; 
	public CustomerManageBean getCustomerManageBean() {
		return customerManageBean;
	}

	public void setCustomerManageBean(CustomerManageBean customerManageBean) {
		this.customerManageBean = customerManageBean;
	}

	public void setLogin(InternetLoginManageBean login) {
		this.login = login;
	}

	String oldPassword, newPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setLoginService(InternetLoginService loginService) {
		this.loginService = loginService;
	}
	
	public String changePass(){
		if(login == null){
			return "login";
		}
		if(newPassword != null && !newPassword.isEmpty() && oldPassword != null && !oldPassword.isEmpty()){
			if(!newPassword.equals(oldPassword)){
				
				if(loginService.changePassword(login.getAcc(), EncryptMD5.md5(oldPassword), EncryptMD5.md5(newPassword))){
					return customerManageBean.showCustomerDetail() ;
				}
				else{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( "Old password is not match"));
				}
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( "New password must not be same with old password"));
			}
		}
		else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage( "Must fill all fields"));
		}
		return "changePassword";
	}
}
